//
//  DJTabBarT.m
//  WeiboDemo
//
//  Created by Bworm on 15/1/28.
//  Copyright (c) 2015年 Bworm. All rights reserved.
//

#import "DJTabBarT.h"
#import "DJTabBarButton.h"

@interface DJTabBarT ()

@property (nonatomic, weak) DJTabBarButton *currentSelBtn;
@property (nonatomic, weak) UIButton *plusButton;
@property (nonatomic, strong) NSMutableArray *btns;
@end

@implementation DJTabBarT

// 通过代码创建控件时会调用
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setup];
    }
    return self;
}

// 通过xib/storyboard创建时调用
- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        [self setup];
    }
    return self;
}

- (void)addItem:(UITabBarItem *)item
{
    DJTabBarButton *btn = [[DJTabBarButton alloc] init];
    
    btn.item = item;
    
    [btn addTarget:self
            action:@selector(btnOnClick:)
  forControlEvents:UIControlEventTouchDown];
    
    [self addSubview:btn];
    
    btn.tag = self.btns.count;
    [self.btns addObject:btn];
    
    if (self.btns.count == 1) {
        [self btnOnClick:btn];
    }
}

- (void)btnOnClick:(DJTabBarButton *)btn
{
    self.currentSelBtn.selected = NO;
    btn.selected = YES;
    
    if ([self.delegate respondsToSelector:@selector(tabBar:selectedBtnFrom:to:)]) {
        [self.delegate tabBar:self selectedBtnFrom:self.currentSelBtn.tag to:btn.tag];
    }
    
    self.currentSelBtn = btn;
    
    
}

- (void)setup
{// 创建 加 图标
//    [self setBackgroundColor:[UIColor whiteColor]];
    
    [self addPlusBtn];
}

- (void)addPlusBtn
{
    UIButton *plusButton = [[UIButton alloc] init];
    plusButton.tag = 213;
    
    [plusButton setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button"]
                          forState:UIControlStateNormal];
    [plusButton setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted"]
                          forState:UIControlStateHighlighted];
    
    [plusButton setImage:[UIImage imageNamed:@"tabbar_compose_icon_add"]
                forState:UIControlStateNormal];
    [plusButton setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_highlighted"]
                forState:UIControlStateHighlighted];
    
    [self addSubview:plusButton];
    
    self.plusButton = plusButton;

}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
//        DDLogDebug(@"%@", [self.subviews firstObject]);
    // 设置4个子控件的frame
    NSUInteger index = 0;
    
    for (UIView *child in self.subviews) {
        // self.subviews 还包含背景跟分割线
        if (child.tag != 213) {
            CGFloat childW = self.frame.size.width / 5;
            CGFloat childH = self.frame.size.height;
            CGFloat childX = index * childW;
            CGFloat childY = 0;
            
            // 判断index是否已经索引到中间的加号控件,若是则跳过,留出空位
            if (index == 2) {
                index++;
                childX = index * childW;
            }
            index++;
            child.frame = CGRectMake(childX, childY, childW, childH);
        }
    }
    // 在此处设置加号按钮的frame
    self.plusButton.size = self.plusButton.currentBackgroundImage.size;
    self.plusButton.centerX = self.width * 0.5;
    self.plusButton.centerY = self.height * 0.5;  
}

- (NSMutableArray *)btns
{
    if (!_btns) {
        _btns = [NSMutableArray array];
    }
    return _btns;
}

@end
